<template>
  <div class="app-container customer-div">
    <el-menu :default-active="listQuery1.type" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="my">我的</el-menu-item>
      <el-menu-item index="all" v-if="purchaseOrderExecutionAll">全部</el-menu-item>
    </el-menu>

    <div style="margin-top:40px">
      <el-form :inline="true" :model="listQuery1" size="mini" label-width="100px">
        <el-form-item label="采购订单号：">
            <el-input v-model="listQuery1.purchaseOrderSn" clearable></el-input>
        </el-form-item>
        <el-form-item label="销售订单号：">
          <el-input v-model="listQuery1.crmOrderSn" clearable></el-input>
        </el-form-item>
        <el-form-item label="商品信息：">
          <el-input v-model="listQuery1.goodsSn" clearable></el-input>
        </el-form-item>
        <el-form-item label="采购日期：">
          <el-date-picker
            v-model="listQuery1.date"
            type="daterange"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="业务员：" v-if="listQuery1.type !='my'">
          <el-select v-model="listQuery1.userId" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in userOptionList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearchList1()"
            type="primary">
            查询
          </el-button>

          <el-button
            v-if="purchaseOrderExecutionExport"
            :loading="btnloading"
            @click="downloadExcel()"
            type="primary">
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--数据列表-->
    <div class="table-container">
      <el-table
        v-loading="listLoading1"
        :data="tableData1"
        max-height="460"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column  label="采购订单号" min-width="180" align="center">
          <template slot-scope="scope">
              {{scope.row.orderSn}}
          </template>
        </el-table-column>
        <el-table-column
          label="供应商"
          min-width="180"
          align="center">
          <template slot-scope="scope">
            {{scope.row.supplierName}}
          </template>
        </el-table-column>
        <el-table-column
          label="合同公司"
          min-width="150"
          align="center">
          <template slot-scope="scope">
            {{scope.row.fromCompany}}
          </template>
        </el-table-column>
        <el-table-column
          label="合同性质"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.contractNature}}
          </template>
        </el-table-column>
        <el-table-column
          label="业务员"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.userName}}
          </template>
        </el-table-column>
        <el-table-column
          label="商品信息"
          min-width="150"
          align="center">
          <template slot-scope="scope">
            {{scope.row.goodsSn}}
          </template>
        </el-table-column>
        <el-table-column
          label="订单数量"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsNum}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="采购单价"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsPrice}}
          </template>
        </el-table-column>
        <el-table-column
          label="采购日期"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.createTime}}
          </template>
        </el-table-column>
        <el-table-column
          label="已入库数量"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsInNum}}
          </template>
        </el-table-column>
        <el-table-column
          label="未入库数量"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsUnInNum}}
          </template>
        </el-table-column>
        <el-table-column
          label="已排货数量"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsTakeNum}}
          </template>
        </el-table-column>
        <el-table-column
          label="已出库数量"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.sendNum}}
          </template>
        </el-table-column>
        <el-table-column
          label="未出库数量"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.unSendNum}}
          </template>
        </el-table-column>

         <el-table-column
          label="最近采购报价"
          align="center"
          min-width="100"
          prop="lastPurchasePrice">
        </el-table-column>
         <el-table-column
          label="最近报价时间"
          align="center"
          min-width="130"
          prop="lastPriceTime">
        </el-table-column>
         <el-table-column
          label="库存价差"
          align="center"
          min-width="100"
          prop="priceDiff">
        </el-table-column>
         <el-table-column
          label="库存亏损预估"
          align="center"
          min-width="100"
          prop="amountDiff">
        </el-table-column>

        <el-table-column
          label="适配销售订单"
          align="center"
          min-width="220">
          <template slot-scope="scope">
            {{scope.row.crmOrderSn}}
          </template>
        </el-table-column>
        <el-table-column
          label="排货销售订单"
          align="center"
          min-width="220">
          <template slot-scope="scope">
            {{scope.row.planOrderSn}}
          </template>
        </el-table-column>
        <el-table-column
          label="实出销售订单"
          align="center"
          min-width="220">
          <template slot-scope="scope">
            {{scope.row.deliverOrderSn}}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange1"
        @current-change="handleCurrentChange1"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery1.pageNum"
        :page-size="listQuery1.pageSize"
        :page-sizes="[10,15,20]"
        :total="count1">
      </el-pagination>
    </div>

  </div>
</template>
<script>
import axios from 'axios'
import {getOrderUsersList} from '@/api/partner.js'
import {getOrderReportList,exportOrderReport} from '@/api/order.js'
import store from '@/store'
import { mapGetters } from 'vuex'
import { formatDate } from '@/utils/date.js';

const defaultListQuery={
  "pageNum": 1,
  "pageSize": 10,
  "type": "my", //my-我的，all-全部
  "purchaseOrderSn": "", //采购订单编码
  "crmOrderSn": "", //销售订单编码
  "goodsSn": "", //商品信息
  "userId": "", //业务员
  "date": [], //采购日期
}

export default {
  name: "purchaseCallReport",
  data(){
    return {
      roles:store.getters.roles ? store.getters.roles : [],
      listQuery1: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData1: [],
      count1: 0,
      listLoading1: false,
      userOptionList:[],
      btnloading:false
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
    purchaseOrderExecutionAll (){
      if(this.roles.indexOf("purchaseOrderExecutionAll") != -1){
        return true
      }else{
        return false
      }
    },
    purchaseOrderExecutionExport (){
      if(this.roles.indexOf("purchaseOrderExecutionExport") != -1){
        return true
      }else{
        return false
      }
    },
  },
  created() {
    this.getUserOptionList();
    let time = formatDate(new Date(), 'yyyy-MM-dd')
    this.listQuery1.date = [time,time]
    this.getList1();
  },
  filters: {


  },
  methods:{
    //下载excle
    downloadExcel(){
      let params = JSON.parse(JSON.stringify(this.listQuery1));
      this.btnloading = true;
      exportOrderReport(params).then((res)=>{
        this.btnloading = false;
        require.ensure([], () => {
          const { export_json_to_excel } = require('sheet/Export2Excel');
          const tHeader = res.title;
          const filterVal = res.columns;
          const list = res.list;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, res.fileName);
        })
      }).catch(()=>{
        this.btnloading = false;
      })
    },

    //下载表格数据格式化
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },

    handleSelect(key){
      this.listQuery1 = JSON.parse(JSON.stringify(defaultListQuery))
      this.listQuery1.type = key
      let time = formatDate(new Date(), 'yyyy-MM-dd')
      this.listQuery1.date = [time,time]
      this.handleSearchList1()
    },

    //获取列表
    getList1(){
      this.listLoading1 = true;
      let listQuery = JSON.parse(JSON.stringify(this.listQuery1));
      getOrderReportList(listQuery).then(response => {
        this.tableData1 = response.list;
        this.count1 = response.count;
        this.listLoading1 = false;
      }).catch(()=>{
        this.listLoading1 = false;
      });
    },

    handleSizeChange1(val){
      this.listQuery1.pageNum = 1;
      this.listQuery1.pageSize = val;
      this.getList1();
    },

    handleCurrentChange1(val){
      this.listQuery1.pageNum = val;
      this.getList1();
    },

    handleSearchList1(){
      this.listQuery1.pageNum = 1;
      this.getList1();
    },

    //获取员工下拉选项
    getUserOptionList(){
      getOrderUsersList().then((res)=>{
        this.userOptionList = res;
      })
    },
  },
}
</script>

<style scoped lang="scss">
.upload-div{
  text-align: center;
  position:relative;
  #upload{

    opacity: 0;
    position: absolute;
    z-index: 1;
    width:154px;
    height:40px
  }
}
.title-tab{
  border-radius: 8px;
  background: #eee;
  margin-left: 10px;
  display: inline-block;
  vertical-align: middle;
  line-height: 20px;
  padding: 0 5px;
  color: #666;
}
</style>
